package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.LimitAdapter;
import com.eeepay.eeepay_v2.model.LimitInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.StatuUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerAddModular;

/**
 * Created by Ching on 2016/3/14.
 * 服务限额界面
 */
public class ServiceLimitActivity extends ABBaseActivity implements TitleBar.LeftBtnOnClickListener, View.OnClickListener {

    private TitleBar titleBar;
    private ListView limitLv;
    private Button comBtn;

    private MerAddModular.ServiceQuota[] datas;
    private List<LimitInfo> list = new ArrayList<>();
    private LimitAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_service_limit;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.tb_service_limit);
        limitLv = getViewById(R.id.lv_service_limit);
        comBtn = getViewById(R.id.btn_limit_complete);
    }

    @Override
    protected void eventOnClick() {

        titleBar.setLeftOnClickListener(this);
        comBtn.setOnClickListener(this);
        adapter = new LimitAdapter(this, comBtn);

        list = SPUtils.getList(Constant.KEY.LIMIT);
        MerAddModular.ServiceQuota quota = null;
        if (list != null && list.size() > 0) {
            LimitInfo limitInfo;
            datas = new MerAddModular.ServiceQuota[list.size()];
            Log.d("serRate", "限额大小： " + datas.length);
            for (int i = 0; i < list.size(); i++) {
                limitInfo = list.get(i);
                quota = new MerAddModular.ServiceQuota();
                quota.serviceName = limitInfo.getServiceName();
                quota.cardType = limitInfo.getCardType();
                quota.holidaysMark = limitInfo.getServiceTime();
                quota.singleMinAmount = limitInfo.getMinAmount();
                quota.singleDayAmount = limitInfo.getDayAmount();
                quota.singleCountAmount = limitInfo.getCountAmount();
                quota.singleDaycardAmount = limitInfo.getDayCardAmount();
                quota.singleDaycardCount = limitInfo.getDayCardConut();
                quota.serviceId = limitInfo.getServiceId();
                quota.fixedQuota = limitInfo.getFixedQuota();
                quota.id = limitInfo.getId();
                datas[i] = quota;
            }
        }
//        else {
////            ("提现", "信用卡", "T0", "5000.00", "200,000,000.00", "50,000,000.00", "5000")
//            datas = new MerAddModular.ServiceQuota[3];
//            for (int i = 0; i < 3; i++) {
//                quota = new MerAddModular.ServiceQuota();
//                quota.serviceName = "提现";
//                quota.cardType = String.valueOf(i);
//                quota.holidaysMark =String.valueOf(i);
//                quota.serviceId=String.valueOf(i);
//                quota.singleCountAmount = "5000.00";
//                quota.singleDayAmount = "200000000.00";
//                quota.singleDaycardCount = "5000";
//                quota.singleDaycardAmount = "50000000.00";
//                datas[i] = quota;
//            }
//        }
        adapter.addAll(datas);
        limitLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        saveLimit(true);
    }

    @Override
    public void onLeftClick(View view) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            onLeftClick(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void saveLimit(boolean showToast) {
        datas = adapter.getDatas();
        MerAddModular.ServiceQuota quota;
        LimitInfo limit;
        list.clear();
        String status;
        Intent intent = new Intent();
        for (int i = 0; i < datas.length; i++) {
            quota = datas[i];
            if (TextUtils.isEmpty(quota.singleCountAmount) || TextUtils.isEmpty(quota.singleDayAmount)
                    || TextUtils.isEmpty(quota.singleDaycardAmount) || TextUtils.isEmpty(quota.singleDaycardCount)) {
                if (showToast) {
                    Toast.makeText(this, "限额填写不完整", Toast.LENGTH_SHORT).show();
                }
                status = "未完成";
                return;
            }
//            if ("1".equals(quota.fixedQuota))//限额不可修改
//                continue;
            limit = new LimitInfo();
            limit.setServiceName(quota.serviceName);
            limit.setCardType(quota.cardType);
            limit.setServiceTime(quota.holidaysMark);
            limit.setServiceId(quota.serviceId);
            limit.setFixedQuota(quota.fixedQuota);
            Log.d("merQuota.id", " merQuota.id:" +  quota.id);
            limit.setId(quota.id);
            limit.setMinAmount(quota.singleMinAmount);
            limit.setCountAmount(quota.singleCountAmount);
            limit.setDayAmount(quota.singleDayAmount);
            limit.setDayCardAmount(quota.singleDaycardAmount);
            limit.setDayCardConut(quota.singleDaycardCount);
            limit.setCardType(quota.cardType);
            limit.setServiceTime(quota.holidaysMark);
            list.add(limit);
//            Log.d("saki", " 单笔最大交易额 ： " + quota.singleCountAmount + " | 单日最大交易额 : " + quota.singleDayAmount
//                    + " | 单日单卡最大交易额 ： " + quota.singleDaycardAmount + " | 单日单卡最大交易笔数 : " + quota.singleDaycardCount);
        }
        SPUtils.saveList(list, Constant.KEY.LIMIT);
        StatuUtils.getInstance().setLimit("已完成");
        status = "已完成";
        intent.putExtra("status", status);
        setResult(RESULT_OK, intent);
        finish();
    }

}
